﻿using System.Collections.Generic;
using Engine;
using Game;
using Mekiasm.Common;
using Mekiasm.FluidElement;

namespace Mekiasm
{
    public class GasTank : ComplexModelItem
    {
        public static int Index;

        public GasTank() : base(new ModelItem[] {
            new ModelItem("valveBase", "chemical_tank"), new ModelItem("valve", null), new ModelItem("tankBase", "chemical_tank"),
            new ModelItem("tank", "chemical_tank"), new ModelItem("rim5", "chemical_tank"), new ModelItem("rim4", "chemical_tank"),
            new ModelItem("rim3", "chemical_tank"), new ModelItem("rim2", "chemical_tank"), new ModelItem("rim1", "chemical_tank"),
            new ModelItem("rim0", "chemical_tank") }, "gastank", "基础气体储罐")
        {
        }

        public override bool OnUse(Ray3 aim, ComponentMiner componentMiner, TerrainRaycastResult? raycast)
        {
            return false;
        }

        public override bool OnInteract(SubsystemBlockBehavior blockBehavior, TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
        {
            int value = componentMiner.ActiveBlockValue;

            int id = Terrain.ExtractContents(value);

            if (BlocksManager.Blocks[id].IsPlaceable_(value))
            {
                return false;
            }
            ComponentGasTank mcomponent = blockBehavior.Project.FindSubsystem<SubsystemBlockEntities>().GetBlockEntity(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z).Entity.FindComponent<ComponentGasTank>(true);
            componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new GasTankWidget(mcomponent, componentMiner);
            return true;
        }

        public override MekBlock.DeviceType GetDeviceType()
        {
            return MekBlock.DeviceType.Gas;
        }

        public override void OnBlockAdded(SubsystemBlockBehavior blockBehavior, int value, int oldValue, int x, int y, int z)
        {
            ILibrary.AddBaseEnergyEntity(DisplayName, x, y, z, new List<ComponentEnergyMachine.Slot>() {
                new ComponentEnergyMachine.Slot(){FuelCapacity=10240,SlotType=ComponentEnergyMachine.SlotType.OUT|ComponentEnergyMachine.SlotType.IN|ComponentEnergyMachine.SlotType.Charge_SlotOut}
            }, ILibrary.GetRotetedConfig(value, new MachineFaceConfig[] {
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Gas),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Gas),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Gas),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Gas),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Gas),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Gas)
            }), new MachineAmountLimitConfig()
            {
                InputGasAmountLimit = 32,
                OutputGasAmountLimit = 32
            });

            GasSystem.Add(new GasTankElement(x,y,z));
        }

        public override void OnBlockGenerated(int value, int x, int y, int z, bool isLoaded)
        {
            GasSystem.Add(new GasTankElement(x, y, z));
        }

        public override void OnBlockRemoved(SubsystemBlockBehavior blockBehavior, int value, int newValue, int x, int y, int z)
        {
            GasSystem.Remove(new Point3(x, y, z));
            ILibrary.RemoveBlockEntity(x, y, z);
        }

    }
}
